package com.lagou.edu.service.impl;

import com.lagou.edu.dao.LagouUserDao;
import com.lagou.edu.pojo.LagouUser;
import com.lagou.edu.service.LagouUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class LagouUserServiceImpl implements LagouUserService {

    @Autowired
    private LagouUserDao userDao;

    /**
     * 保存发送的验证码
     *
     * @param user
     * @return
     */
    @Override
    public Boolean saveUser(LagouUser user) {
        LagouUser result = userDao.save(user);
        return result.getId() != 0;
    }

    /**
     * 获取用户信息
     *
     * @param email
     * @param password
     * @return
     */
    @Override
    public LagouUser findOne(String email, String password) {
        LagouUser user = LagouUser.builder().email(email).password(password).build();

        Example<? extends LagouUser> example = Example.of(user);
        Optional<? extends LagouUser> userOptional = userDao.findOne(example);
        if (userOptional.isPresent()) {
            return userOptional.get();
        }
        return null;
    }

    /**
     * 获取用户信息
     *
     * @param email
     * @return
     */
    @Override
    public LagouUser findOne(String email) {
        LagouUser user = LagouUser.builder().email(email).build();

        Example<? extends LagouUser> example = Example.of(user);
        Optional<? extends LagouUser> userOptional = userDao.findOne(example);
        if (userOptional.isPresent()) {
            return userOptional.get();
        }
        return null;
    }
}
